package com.lianjia.sh.tools.msg.v1.sender.impl;
import com.dooioo.se.client.RestClient;
import com.dooioo.se.client.RestClientException;
import com.lianjia.sh.tools.msg.v1.model.*;
import com.lianjia.sh.tools.msg.v1.sender.PnsSender;
import org.springframework.http.HttpMethod;
import org.springframework.util.Assert;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by yangsp1 on 16/6/24.
 */
public class PnsSenderImpl implements PnsSender {
    String pnsTextUrl;
    String pnsRichTextUrl;
    String pnsPicUrl;

    public boolean isMatch(Msg msg){
        return  msg instanceof PnsMsg;
    }

    @Override
    public boolean send(Msg msg) {
        Assert.notNull(msg);
        Assert.isTrue(isMatch(msg));
        if(msg instanceof PnsTextMsg){
            return send((PnsTextMsg)msg);
        }else if(msg instanceof PnsRichTextMsg){
            return send((PnsRichTextMsg)msg);
        }else if(msg instanceof PnsPicMsg){
            return send((PnsPicMsg)msg);
        }
        return false;
    }

    public boolean send(PnsTextMsg pnsTextMsg){
        System.out.println("发送PNS普通文本：" + pnsTextMsg.getRemark());
        Map<String,Object> params = new HashMap<>();
        params.put("businessId",pnsTextMsg.getBusinessId());
        params.put("appId",pnsTextMsg.getAppId());
        params.put("remark",pnsTextMsg.getRemark());
        params.put("prompt",pnsTextMsg.getRemark());
        params.put("touchdown",pnsTextMsg.getTouchdown());
        params.put("recipients",pnsTextMsg.getRecipients());//发送给谁
        return send(pnsTextUrl,params);
    }

    public boolean send(PnsRichTextMsg pnsRichTextMsg){
        System.out.println("发送PNS富文本：" + pnsRichTextMsg.getRemark());

        Map<String,Object> params = new HashMap<>();
        params.put("businessId",pnsRichTextMsg.getBusinessId());
        params.put("richType",pnsRichTextMsg.getRichType());
        params.put("linkId",pnsRichTextMsg.getLinkId());
        params.put("title",pnsRichTextMsg.getTitle());
        params.put("remark",pnsRichTextMsg.getRemark());
        params.put("subTitle",pnsRichTextMsg.getSubTitle());
        params.put("time",pnsRichTextMsg.getTitle());
        params.put("recipients",pnsRichTextMsg.getRecipients());//发送给谁
        return send(pnsRichTextUrl,params);
    }
    public boolean send(PnsPicMsg pnsPicMsg){
        System.out.println("发送PNS图片：" + pnsPicMsg.getPicUrl());
        Map<String,Object> params = new HashMap<>();
        params.put("picUrl",pnsPicMsg.getPicUrl());
        params.put("recipients",pnsPicMsg.getRecipients());//发送给谁
        return send(pnsPicUrl,params);
    }

    public boolean send(String url,Map<String,Object> params){
        try {
            RestClient.exchange(url, HttpMethod.POST, String.class, params);
        } catch (RestClientException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    @Override
    public String getPnsTextUrl() {
        return pnsTextUrl;
    }

    @Override
    public void setPnsTextUrl(String pnsTextUrl) {
        this.pnsTextUrl = pnsTextUrl;
    }

    @Override
    public String getPnsRichTextUrl() {
        return pnsRichTextUrl;
    }

    @Override
    public void setPnsRichTextUrl(String pnsRichTextUrl) {
        this.pnsRichTextUrl = pnsRichTextUrl;
    }

    @Override
    public String getPnsPicUrl() {
        return pnsPicUrl;
    }

    @Override
    public void setPnsPicUrl(String pnsPicUrl) {
        this.pnsPicUrl = pnsPicUrl;
    }
}
